package com.sfh.cpc.home.fragment.view;

import android.arch.lifecycle.Lifecycle;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cpc.R;
import com.sfh.cpc.com.BaseAppContent;
import com.sfh.cpc.com.ImageLoaderEngine;
import com.sfh.cpc.com.entity.EMass;
import com.sfh.cpc.com.widget.SpaceItemLeftDecoration;
import com.sfh.cpc.home.adapter.HomeMassTopAdapter;
import com.sfh.cpc.mass.MassActivity;
import com.sfh.cpc.home.model.HomeViewModel;
import com.sfh.lib.mvvm.annotation.LiveDataMatch;
import com.sfh.lib.ui.AbstractLifecycleFragment;

import java.util.List;

/**
 * 功能描述: 推荐
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */
public class MassRecommendFragmentView extends AbstractLifecycleFragment<HomeViewModel> {

    public static Fragment newFragment(int index) {
        Fragment fragment =  new MassRecommendFragmentView();
        Bundle data = new Bundle();
        data.putInt("INDEX",index);
        fragment.setArguments(data);
        return fragment;
    }

    private HomeMassTopAdapter adapter;
    private ImageView ivTopImage;
    private TextView tvTopTitle;
    private TextView tvTopRq;
    private RecyclerView swipeRefreshMoreLayout;
    private EMass mass;

    @Override
    public int getLayout() {
        return R.layout.home_mass_a_fragment;
    }

    @Override
    public void initData(View view) {
        ivTopImage = view.findViewById(R.id.iv_top_image);
        tvTopTitle = view.findViewById(R.id.tv_top_title);
        tvTopRq = view.findViewById(R.id.tv_top_rq);
        swipeRefreshMoreLayout = view.findViewById(R.id.recyclerView);

        adapter = new HomeMassTopAdapter();
        LinearLayoutManager gridLayoutManager = new LinearLayoutManager(getContext());
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        gridLayoutManager.setSmoothScrollbarEnabled(true);

        swipeRefreshMoreLayout.setLayoutManager(gridLayoutManager);
        swipeRefreshMoreLayout.addItemDecoration(new SpaceItemLeftDecoration(21));
        swipeRefreshMoreLayout.setAdapter(adapter);

      final  int index = getArguments().getInt("INDEX",0);
        view.findViewById(R.id.tv_change).setVisibility(index == 0?View.VISIBLE:View.GONE);

        ivTopImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MassActivity.startUI(getContext(), mass);
            }
        });
        view.findViewById(R.id.tv_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
                getViewModel().queryHomeRecomMass(index);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                EMass mass = (EMass) adapter.getData().get(position);
                MassActivity.startUI(getContext(), mass);
            }
        });
        getViewModel().queryHomeRecomMass(index);
    }


    @LiveDataMatch
    public void onMassListSuccess(List<EMass> data) {

        if (data != null) {
            mass = data.get(0);
            new ImageLoaderEngine(getContext(),  BaseAppContent.getImageUrl(mass.getMassUrl())).roundTransformCenterCrop(7).into(ivTopImage);
            tvTopTitle.setText(mass.getMassTitle());
            tvTopRq.setText(mass.getMassContent());
            data.remove(0);
            adapter.setNewData(data);
        }
    }
}
